cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
cmake_policy(SET CMP0054 NEW)

set(PROJECT_VERSION 0.5.1)
project(SpireMS VERSION ${PROJECT_VERSION} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)

add_definitions(-DAPI_EXPORTS)
set(CMAKE_BUILD_TYPE "Release")


find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
find_library(RT_LIBRARY rt)
if(RT_LIBRARY)
    message(STATUS "Found librt: ${RT_LIBRARY}")
else()
    message(WARNING "librt not found, shm_open may not work")
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)


# Public header
set(
  public_HEADS
  include/sms_core.h
  include/sms_helper.h
  include/sms_publisher.h
  include/sms_subscriber.h
  include/sms_parameter.h
  include/sms_base_node.h
  include/sms_service.h
  include/sms_client.h
  include/sms_json.hpp
  include/sms_base64.hpp
)

# SpireMS source
set(spirems_SRCS
  src/sms_helper.cpp
  src/sms_publisher.cpp
  src/sms_subscriber.cpp
  src/sms_parameter.cpp
  src/sms_base_node.cpp
  src/sms_service.cpp
  src/sms_client.cpp
)

add_library(sms_core SHARED ${spirems_SRCS})
target_link_libraries(
  sms_core
  ${OpenCV_LIBS}
  ${RT_LIBRARY}
)


# Demo
add_executable(PublisherDemo demo/publisher_demo.cpp)
target_link_libraries(PublisherDemo sms_core)
add_executable(SubscriberDemo demo/subscriber_demo.cpp)
target_link_libraries(SubscriberDemo sms_core)

add_executable(StrSenderTest demo/str_sender_test.cpp)
target_link_libraries(StrSenderTest sms_core)
add_executable(StrReceiverTest demo/str_receiver_test.cpp)
target_link_libraries(StrReceiverTest sms_core)

add_executable(ImageSenderTest demo/image_sender_test.cpp)
target_link_libraries(ImageSenderTest sms_core)
add_executable(ImageReceiverTest demo/image_receiver_test.cpp)
target_link_libraries(ImageReceiverTest sms_core)

add_executable(PoseSenderTest demo/pose_sender_test.cpp)
target_link_libraries(PoseSenderTest sms_core)
add_executable(PoseReceiverTest demo/pose_receiver_test.cpp)
target_link_libraries(PoseReceiverTest sms_core)

add_executable(AnySenderTest demo/any_sender_test.cpp)
target_link_libraries(AnySenderTest sms_core)
add_executable(AnyReceiverTest demo/any_receiver_test.cpp)
target_link_libraries(AnyReceiverTest sms_core)

add_executable(ParameterTest demo/parameter_test.cpp)
target_link_libraries(ParameterTest sms_core)

add_executable(POSIXRead demo/posix_read.cpp)
target_link_libraries(POSIXRead sms_core)

add_executable(POSIXShare demo/posix_share.cpp)
target_link_libraries(POSIXShare sms_core)

add_executable(ShareMemReceiverTest demo/share_mem_receiver_test.cpp)
target_link_libraries(ShareMemReceiverTest sms_core)

add_executable(ShareMemSenderTest demo/share_mem_sender_test.cpp)
target_link_libraries(ShareMemSenderTest sms_core)

add_executable(ShareMemMTStressTestSender demo/share_mem_mt_stress_test_sender.cpp)
target_link_libraries(ShareMemMTStressTestSender sms_core)

add_executable(ShareMemMTStressTestReceiver demo/share_mem_mt_stress_test_receiver.cpp)
target_link_libraries(ShareMemMTStressTestReceiver sms_core)

add_executable(Str2StrServiceTest demo/str2str_service_test.cpp)
target_link_libraries(Str2StrServiceTest sms_core)

add_executable(Str2StrClientTest demo/str2str_client_test.cpp)
target_link_libraries(Str2StrClientTest sms_core)

add_executable(CSVLoggerTest demo/csv_logger_test.cpp)
target_link_libraries(CSVLoggerTest sms_core)

add_executable(ShareMemPointcloudTransfer demo/share_mem_pointcloud_transfer.cpp)
target_link_libraries(ShareMemPointcloudTransfer sms_core)

# Install
install(TARGETS sms_core
  LIBRARY DESTINATION lib
)
install(FILES ${public_HEADS}
  DESTINATION include
)


message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
if (NOT DEFINED SpireMS_INSTALL_PREFIX)
  set(SpireMS_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
  message(STATUS "SpireMS_INSTALL_PREFIX: ${SpireMS_INSTALL_PREFIX}")
else()
  message(STATUS "SpireMS_INSTALL_PREFIX: ${SpireMS_INSTALL_PREFIX}")
endif()


file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/build/${PROJECT_NAME}Config.cmake.in [[
@PACKAGE_INIT@
find_package(OpenCV REQUIRED)

set(SpireMS_INCLUDE_DIRS
  @SpireMS_INSTALL_PREFIX@/include
  ${OpenCV_INCLUDE_DIRS}
)
set(SpireMS_LIBS
  @SpireMS_INSTALL_PREFIX@/lib/libsms_core.so
  ${OpenCV_LIBS}
  rt pthread
)
]])


include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config-version.cmake
  VERSION ${PROJECT_VERSION}
  COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(${CMAKE_CURRENT_BINARY_DIR}/build/${PROJECT_NAME}Config.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}
)
install(FILES 
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config-version.cmake
  DESTINATION lib/cmake/${PROJECT_NAME}
)


file(GLOB JSON_FILES_1 "${CMAKE_SOURCE_DIR}/../spirems/msgs/_config_msgs/*.json")
install(FILES 
  ${JSON_FILES_1}
  DESTINATION /usr/share/spirems/msgs/_config_msgs
)

file(GLOB JSON_FILES_2 "${CMAKE_SOURCE_DIR}/../spirems/msgs/_sys_msgs/*.json")
install(FILES 
  ${JSON_FILES_2}
  DESTINATION /usr/share/spirems/msgs/_sys_msgs
)

file(GLOB JSON_FILES_3 "${CMAKE_SOURCE_DIR}/../spirems/msgs/_visual_msgs/*.json")
install(FILES 
  ${JSON_FILES_3}
  DESTINATION /usr/share/spirems/msgs/_visual_msgs
)

file(GLOB JSON_FILES_4 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/geometry_msgs/*.json")
install(FILES 
  ${JSON_FILES_4}
  DESTINATION /usr/share/spirems/json_msgs/geometry_msgs
)

file(GLOB JSON_FILES_5 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/memory_msgs/*.json")
install(FILES 
  ${JSON_FILES_5}
  DESTINATION /usr/share/spirems/json_msgs/memory_msgs
)

file(GLOB JSON_FILES_6 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/sensor_msgs/*.json")
install(FILES 
  ${JSON_FILES_6}
  DESTINATION /usr/share/spirems/json_msgs/sensor_msgs
)

file(GLOB JSON_FILES_7 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/spirecv_msgs/*.json")
install(FILES 
  ${JSON_FILES_7}
  DESTINATION /usr/share/spirems/json_msgs/spirecv_msgs
)

file(GLOB JSON_FILES_8 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/std_msgs/*.json")
install(FILES 
  ${JSON_FILES_8}
  DESTINATION /usr/share/spirems/json_msgs/std_msgs
)

file(GLOB JSON_FILES_8 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/nav_msgs/*.json")
install(FILES 
  ${JSON_FILES_8}
  DESTINATION /usr/share/spirems/json_msgs/nav_msgs
)

file(GLOB JSON_FILES_8 "${CMAKE_SOURCE_DIR}/../spirems/json_msgs/vehicle_msgs/*.json")
install(FILES 
  ${JSON_FILES_8}
  DESTINATION /usr/share/spirems/json_msgs/vehicle_msgs
)
